package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import model_jpa.*;

public class DAOCliente extends DAOGenerico<Cliente> {

	public Cliente inserirCliente(Cliente c) {
		int result = 0;
		Statement stmt;
		PreparedStatement pstmt;
		Connection conn = Conexao.getConnection();
		String sql = null;
		try {
			sql = "insert into cliente(nomeCliente, CPFCliente) values (?,?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, c.getNomeCliente());
			pstmt.setString(2, c.getCpfcliente());
			
			result = pstmt.executeUpdate();
			System.out.println(result);
			
			sql = "select idCliente from cliente where nomeCliente = '"+c.getNomeCliente()+"' and CPFCliente = '"+c.getCpfcliente()+"'";
			stmt = conn.createStatement(); 
			ResultSet rs = stmt.executeQuery(sql);
		
			while(rs.next()){
				int idCliente = rs.getInt("idCliente");
				c.setIdCliente(idCliente);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return c;
	}
	public void inserirClienteJPA(Cliente c) {
		inserir(c);
	}
	public static void main(String[] args) {
		Cliente c = new Cliente();
		
		c.setNomeCliente("meu nome");
		c.setCpfcliente("111111111");
		
		DAOCliente dao = new DAOCliente();
		dao.inserir(c);
	}

}
